<template>
	<view class="container">
		<view class="search-box">
			<uni-search-bar @input="input" @confirm="searchGoods(kw)" :radius="100" cancelButton="none"></uni-search-bar>
			<view class="search-buttom" @click="searchGoods(kw)">
				搜索
			</view>
		</view>
		<view class="history-box" v-if="searchResult.length===0">
			<view class="history-title">
				<text>搜索历史</text>
				<uni-icons type="trash-filled" size="23" @click="cleanHistory"></uni-icons>
			</view>
			<view class="history-list">
				<view class="history-item" v-for="(item,i) in historys" :key="i" @click="searchGoods(item)">
					{{item}}
				</view>
			</view>
		</view>
		<view class="result-list" v-else v-for="(item,i) in searchResult" :key="i">
			<view class="result-item" @click="gotoDetail(item.goods_id)">
				<view class="goods-name">{{item.goods_name}}</view>
				<uni-icons type="arrowright" size="16"></uni-icons>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				timer: null,
				kw: '',
				historyList:[],
				searchResult: [],
			};
		},
		computed:{
			historys(){
				return [...this.historyList].reverse()
			}
		},
		methods:{
			input(e) {
				// console.log(e)
			  // 清除 timer 对应的延时器
			  clearTimeout(this.timer)
			  // 重新启动一个延时器，并把 timerId 赋值给 this.timer
			  this.timer = setTimeout(() => {
			    // 如果 500 毫秒内，没有触发新的输入事件，则为搜索关键词赋值
			    this.kw = e
				this.getSearchResult();
			    // console.log(this.kw)
			  }, 100)
			},
			
			//获取搜索建议
			async getSearchResult(){
				if(this.kw === '' ){
					this.searchResult = []
					return ;
				}
				const {data : res} = await uni.$http.get('/api/public/v1/goods/qsearch', { query: this.kw })
				if(res.meta.status !== 200) return uni.$showMsg()
				this.searchResult = res.message;
				// console.log(this.searchResult)
			},
			
			//点击进入商品详情页
			gotoDetail(goods_id) {
				this.saveSearchHistory()
				uni.navigateTo({
					url:'/subpkg/goods_detail/goods_detail?goods_id=' + goods_id
				})
			},
			
			
			//搜索事件
			searchGoods(kw){
				if(kw === '' ){
					return uni.$showMsg('搜索不能为空')
				}
				uni.navigateTo({
					url:'/subpkg/goods_list/goods_list?query=' + kw
				})
				// push浏览搜索记录
				if(!this.historyList.includes(this.kw) && this.kw){
					this.historyList.push(this.kw)
					uni.setStorageSync('giao', JSON.stringify(this.historyList))
				}
			},
			
			// 清空搜索历史记录
			cleanHistory() {
			  // 清空 data 中保存的搜索历史
			  this.historyList = []
			  // 清空本地存储中记录的搜索历史
			  uni.setStorageSync('giao',[])
			  console.log('已清除')
			},
		},
		
		onLoad() {
			//加载本地存储的浏览记录
			this.historyList = JSON.parse(uni.getStorageSync('giao') || '[]')
			// const res = uni.getStorageInfoSync();
				// console.log(this.historyList);
				// console.log('牛逼')
			// console.log(uni.getStorageSync('his'))
		}
	}
</script>

<style lang="scss">

	
	.search-box{
		display: flex;
		justify-content: space-between;
		position: sticky;
		top: 0;
		z-index: 999;
		position: relative;
		background-color: #c00000;
			
		uni-search-bar {
			width: 80%;
		}
		
		.uni-searchbar {
			padding: 16rpx;
			margin-top: -4px;
			width: 100%;
			height: 34px;
			/* 将默认的 #FFFFFF 改为 #C00000 */
			// background-color: #c00000;
		}
		
		.search-buttom{
			position: absolute;
			top:0;
			right: 0;
			width: 18%;
			text-align: center;
			// align-items: center;	
			color: #fff;
			font-weight: bold;
			padding-top: 4px;
			line-height: 34px;
			// background-color: #c00000;
		}
	}
	
	.history-box {
		margin: 0 8px;
		.history-title {
			margin: 14px 6px 0;
			padding-bottom: 6px;
			font-size: 14px;
			align-items: center;
			display: flex;
			justify-content: space-between;
			// border-bottom: 1px #eaeaea solid;
			border-bottom: 1px solid #ccc;
		}
		
		.history-list{
			display: flex;
			flex-wrap: wrap;
			// justify-content: space-between;
			.history-item {
				// display: block;
				padding: 4px;
				font-size: 12px;
				// background-color: #eee;
				border: 1px solid #ccc;
				border-radius: 8px;
				margin: 10px 5px;
			}
		}
	}
	
	.result-item {
	    font-size: 12px;
	    padding: 13px 0;
	    border-bottom: 1px solid #eaeaea;
	    display: flex;
	    align-items: center;
	    justify-content: space-between;
		
		 .goods-name {
		      // 文字不允许换行（单行文本）
		      white-space: nowrap;
		      // 溢出部分隐藏
		      overflow: hidden;
		      // 文本溢出后，使用 ... 代替
		      text-overflow: ellipsis;
		      margin: 0 3px;
		    }
	}
</style>
